﻿using System;
using System.Web.Services.Protocols;
using MedicWeb.MedicService;

namespace MedicWeb.forms.Editors.Directory {
    public partial class SpecialityEdit : SessionPage {

        private Speciality speciality;

        protected void Page_Load(object sender, EventArgs e) {
            string id = Request.QueryString["id"];
            Form.Action = ResolveUrl("~/forms/Editors/Directory/SpecialityEdit.aspx");
            var ms = new MainService();
            if (id != null) {
                Form.Action += "?id=" + id;
                speciality = ms.LoadSpeciality(GetSession(), int.Parse(id));
            } else {
                speciality = new Speciality();
                speciality.Title = "";
                speciality.ClassSpecialisations = new ClassSpecialisation[0];
            }
            tfId.Text = "" + speciality.Id;
            if (!IsPostBack) {
                tfTitle.Text = speciality.Title;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            try {
                var ms = new MainService();
                speciality.Title = tfTitle.Text;
                ms.SaveSpeciality(GetSession(), speciality);
                Session["success"] = "Изменения сохранены успешно";
            } catch (Exception ex) {

                if (ex.InnerException is SoapException
                    && ((SoapException)ex.InnerException).Detail.InnerText == "AuthorizationException") {
                    throw ex;
                } else {
                    Session["error"] = "Ошибка сохранения";
                }
            }
        }
    }
}